package com.infore.siteManagement.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.constant.ResultConstants;
import com.infore.siteManagement.entity.Basinn;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.repository.IBasinnRespository;
import com.infore.siteManagement.select.QueryDispatcher;
import com.infore.siteManagement.select.dto.QueryParamDto;
import com.infore.siteManagement.select.util.QueryUtil;
import com.infore.siteManagement.service.IBasinnService;
import com.infore.siteManagement.util.MyBeanUtils;
import com.infore.siteManagement.util.Page;
import com.infore.siteManagement.util.PageUtil;
import com.infore.siteManagement.util.ResultEntity;
/**
 * 流域管理服务实现层
 * @author 刘乐法
 *
 *
 */
@Service
public class IBasinnServiceImpl implements IBasinnService{

	@Autowired
	private IBasinnRespository iBasinnRespository;
	@Autowired
	private QueryDispatcher queryDispatcher;
	
	@Override
	public ResultEntity queryBasinn(QueryParamDto queryParamDto) {
		if (org.springframework.util.StringUtils.isEmpty(queryParamDto)) {//无任何条件查询
			List<Basinn> list = iBasinnRespository.findAll();
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		//仅分页查询
		}else if (!org.springframework.util.StringUtils.isEmpty(queryParamDto.getPageObject()) && 
				org.springframework.util.StringUtils.isEmpty(queryParamDto.getQueryCondition()) &&
				org.springframework.util.StringUtils.isEmpty(queryParamDto.getAssembleCondition()) && 
				org.springframework.util.StringUtils.isEmpty(queryParamDto.getAssembleSql())) {
			org.springframework.data.domain.Page<Basinn> pageable = iBasinnRespository.findAll(queryParamDto.getPageObject().getPageRequest());
			Page<Basinn> page = PageUtil.pageConvert(pageable);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		//只传属性不传字段的查询
		}else if (org.apache.commons.lang.StringUtils.isBlank(queryParamDto.getAssembleSql().getSelect())) {
			return this.queryAll(queryParamDto);
		}
		//指定字段查询
		return queryDispatcher.dispatcher(queryParamDto, Basinn.class);
	}
	
	@SuppressWarnings("serial")
	public ResultEntity queryAll(QueryParamDto queryParamDto) {
		//判断是否有条件再判断是否需要分页,不分页必须传null
		if (!org.springframework.util.StringUtils.isEmpty(queryParamDto.getPageObject())) {
			//需要分页
			org.springframework.data.domain.Page<Basinn> pageAble =  iBasinnRespository.findAll(new Specification<Basinn>() {

				@Override
				public Predicate toPredicate(Root<Basinn> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			}, queryParamDto.getPageObject().getPageRequest());
			Page<Basinn> page = PageUtil.pageConvert(pageAble);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		}else {
			List<Basinn> list = iBasinnRespository.findAll(new Specification<Basinn>() {

				@Override
				public Predicate toPredicate(Root<Basinn> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			});
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		}
	}


	@Override
	public ResultEntity deleteBasinn(String ids) {
		if (ids.contains(",")) {
			List<String> idsList = Arrays.asList(ids.split(","));
			iBasinnRespository.deleteByBasinnIdIn(idsList);
		} else {
			iBasinnRespository.deleteById(ids);
		}
		return new ResultEntity(ResultStatus.SUCCESS,Constants.DELETE_SUCCESS);
	}

	@Override
	public ResultEntity editBasinn(Basinn basinn) {
		//如果id不为空则编辑 flag=false
		//flag用来判断返回的Constants.UPDATE_SUCCESS还是Constants.INSERT_SUCCESS
		Boolean flag = false;
		if (StringUtils.isNotBlank(basinn.getBasinnId())) {
			Basinn basinnOld = iBasinnRespository.findById(basinn.getBasinnId()).orElse(null);
			if (basinnOld != null) {
				MyBeanUtils.copyNonNullProperties(basinn, basinnOld);
				basinn = basinnOld;
				flag = true;
			}else {
				return new ResultEntity(ResultStatus.ERROR,ResultConstants.NO_SUCH_OBJECT);
			}
		}
		ResultEntity resultEntity = validateBasinn(basinn);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		try {
			iBasinnRespository.saveAndFlush(basinn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (flag) {
			return new ResultEntity(ResultStatus.SUCCESS,Constants.UPDATE_SUCCESS);
		} else {
			return new ResultEntity(ResultStatus.SUCCESS,Constants.INSERT_SUCCESS);
		}
	}

	@Override
	public ResultEntity validateBasinn(Basinn basinn) {
		if (basinn.getBasinnRemark().length() > 500) {
			return new ResultEntity(ResultStatus.ERROR,"流域备注过长");
		}else if(basinn.getBasinnName().length() > 50){
			return new ResultEntity(ResultStatus.ERROR,"流域名称过长");
		}else if(org.springframework.util.StringUtils.isEmpty(basinn.getBasinnName())){
			return new ResultEntity(ResultStatus.ERROR,"流域名称不能为空");
		}
		return new ResultEntity(ResultStatus.SUCCESS,"流域验证成功");
	}

}
